home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TeX 1995 July
/
TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO
/
graphics
/
piechart
/
piechart.awk
< prev
next >
Wrap
Text File
|
1992-10-25
|
7KB
|
174 lines
#! /bin/awk
# -*- Mode: Awk -*-
# piecharts.awk --- Creation of pie charts in PostScript, via LaTeX and
PSTricks
# Author : Denis GIROU (CNRS/CIRCE - France) <girou@circe.fr>
# Created the : Fri Oct 16 21:53:41 1992
# Last mod. by : Denis GIROU (CNRS/CIRCE - France) <girou@circe.fr>
# Last mod. the : Tue Oct 20 20:03:39 1992
# Version : 1.0
#
# Description : piecharts.awk is a simple AWK program to realize hight
# quality, greatly customizable, grayscaled or colored pie
# charts in PostScript.
# It uses LaTeX and the splendid pstricks package of
# Timothy Van Zandt <tvz@princeton.edu>, available in
# /anonymous@princeton.edu:/pub
# It creates the LaTeX (+ PSTricks) code for direct
# insertion in a LaTeX document of the pie chart generated.
# It use a data file which describe the parts and the labels
# for them.
# You always can modify the generated code for more
# sophisticated result.
#
# Syntax : awk -f piecharts.awk [-v SCALE=scale] [-v BOXIT=boxit]
# <data_file >output_file
# Default scale factor is 1.
# Use BOXIT=boxit if you want a box around the figure.
#
# Input file : The fill styles are: none, solid, vlines, vlines*,
# hlines, hlines*, crosshatch, crosshatch*
# The predefined colors are : black, darkgray, gray,
# lightgray, white, red, green, blue, cyan, magenta, yellow
# You can easily define new gray or colors, or access to a
# rgb palette with the palette.sty file of PSTricks.
# You can also change a lot of parameters. See the
# documentation of PSTricks for more customization.
#
# Input file format: You can add blanks lines and comments (beginning by #).
# The default field separator is | You can change it at
# the awk level.
# The first non-comment line has 3 fields for titles, and
# after you must have one line by part of the pie chart,
# with 5 fields each (percentage, inside label, outside
# label, filling, color).
# Evidentely, the total of the percentages must be 360, or
# less.
# You can add LaTeX commands in the labels (\em, \small,
# etc.).
# Be careful of some LaTeX special characters (see $ and %
# in the examples).
# Here are two examples (suppress the first 4 characters
# of each line):
#
# # Data for the first example of pie charts
#
# # Title of the figure | Title for the eventual table of figures
| Label
# \bf Example 1 of pie chart | Pie Chart 1
| piechart1
#
# # Value | Inside label | Outside label | Type of filling | Color
of filling
# 70 | \small\$9.0M | {\em Paris} | hlines | lightgray
# 130 | \small\$16.70M | {\em London} | vlines | gray
# 160 | \small\$23.1M | {\em Berlin} | crosshatch | black
#
#
# # Data for the second example of pie charts
#
# # Title of the figure | Title for the eventual table of figures
| Label
# \bf Example 2 of pie chart | Pie Chart 2
| piechart2
#
# # Value | Inside label | Outside label | Type of filling | Color of filling
# 16 | | 16\% | solid | red
# 113 | | 113\% | solid | blue
# 6 | | 6\% | solid | green
# 149 | | 149\% | solid | magenta
# 76 | | 76\% | solid | cyan
#
#
# Examples : If you name these files piechart1.dat and piechart2.dat,
# you can simply type:
# awk -f piecharts.awk -v SCALE=1.5 <piechart1.dat >piechart1.tmp
# awk -f piecharts.awk -v BOXIT=boxit -v SCALE=0.7 <piechart2.dat
>piechart2.tmp
# and just insert the two result files in a LaTeX document
# at the right place...
#
# Portability : Expected great. Tested on an RS6000 under AIX 3.2.
#
# Known problems : Evidentely, take care of the labels for the small parts.
# If they are too long or in a too big font, the result will
# not be pretty...
# Beginning of the AWK program
BEGIN{
FS="|" # Fields separator
PARAM="NO"
BEGIN_VALUE=0
TOTAL=0
if (SCALE == "")
SCALE=1.
print "\\begin{figure}[htbp]"
# Framebox around the figure, or not
if (BOXIT == "boxit" || BOXIT == "BOXIT" )
print " \\psframebox{"
print " \\begin{minipage}{\\textwidth}"
print " \\begin{center}"
if (BOXIT == "boxit" || BOXIT == "BOXIT" )
print " \\vspace{5mm}"
print " \\psset{unit=" SCALE "}"
print " \\begin{pspicture}(-2,-2)(2,2)"
print " \\Polar"
print " \\psset{framesep=1.5pt}"
}
{
# Comments and blanks lines
if (substr($1,1,1) == "#" || $1 == "")
break
# Suppression of the right and left blanks of the variables
for (i=1;i<=NF;i++) {
TEMP = sub(/^ */,"",$i)
TEMP = sub(/ *$/,"",$i)}
if (PARAM == "NO") {
# Generic parameters : title, title for the eventual table of figures, label
PARAM="YES"
TITLE=$1
TABLE_TITLE=$2
LABEL=$3}
else {
TOTAL=TOTAL+$1
print " \\pswedge[fillstyle=" $4 ",fillcolor=" $5 "]{2}{"
BEGIN_VALUE "}{" TOTAL "}"
if (TOTAL > 360) {
print "Error ! Total of percentages greater than 360! (" TOTAL ")"
exit}
VAL=BEGIN_VALUE+($1-val_pourc)/2
# Inside label
if ($2 != "")
print " \\rput(1.2," VAL ") {\\psframebox*{" $2 "}}"
# Outside label
if ($3 != "") {
if (VAL < 91)
POSITION="bl"
else if (VAL < 181)
POSITION="br"
else if (VAL < 271)
POSITION="tr"
else POSITION="tl"
print " \\rput[" POSITION "](2.2," VAL ") {" $3 "}"}
BEGIN_VALUE=TOTAL}
}
END{
print " \\end{pspicture}"
print " \\end{center}"
print " \\caption[" TABLE_TITLE "]{" TITLE "}"
print " \\label{f:" LABEL "}"
print " \\end{minipage}"
if (BOXIT == "boxit" || BOXIT == "BOXIT" )
print " }"
print "\\end{figure}"
}